import Storage from 'web-storage-cache';

const localStorage = new Storage();

// *将数据进行本地存储
export function setLocalStorage(key, value) {
  return localStorage.set(key, value);
}

// *获取本地存储数据
export function getLocalStorage(key) {
  return localStorage.get(key);
}

// *删除某个本地存储
export function removeLocalStorage(key) {
  return localStorage.delete(key);
}

// *删除全部本地存储
export function clearLocalStorage() {
  return localStorage.clear();
}

// *保存书架数据
export function saveBookShelf(shelf) {
  return setLocalStorage('shelf', shelf);
}

export function getBookShelf() {
  return getLocalStorage('shelf');
}

// *存储当前电子书的信息
export function setBookObject(fileName, key, value) {
  // #判断当前电子书是否存在
  let book = getLocalStorage(`${fileName}-info`);

  if (!book) {
    book = {};
  }
  book[key] = value;

  // #本地存储电子书信息
  setLocalStorage(`${fileName}-info`, book);
}

// *获取电子书的信息
export function getBookObject(fileName, key) {
  let book = getLocalStorage(`${fileName}-info`);
  if (book) {
    return book[key];
  } else {
    return null;
  }
}

/*
 *获取、存储用户使用的字体类型
 */
export function getFontFamily(fileName) {
  return getBookObject(fileName, 'fontFamily');
}

export function saveFontFamily(fileName, font) {
  return setBookObject(fileName, 'fontFamily', font);
}

/*
 *获取、存储用户使用的语言类型
 */
export function getLocale() {
  return getLocalStorage('locale');
}

export function saveLocale(locale) {
  return setLocalStorage('locale', locale);
}

/*
 */
export function getLocation(fileName) {
  return getBookObject(fileName, 'location');
}

export function saveLocation(fileName, location) {
  setBookObject(fileName, 'location', location);
}

export function getBookmark(fileName) {
  return getBookObject(fileName, 'bookmark');
}

export function saveBookmark(fileName, bookmark) {
  setBookObject(fileName, 'bookmark', bookmark);
}

/*
 *读取/存储用户阅读时间
 */
export function getReadTime(fileName) {
  return getBookObject(fileName, 'time');
}

export function saveReadTime(fileName, theme) {
  setBookObject(fileName, 'time', theme);
}

/*
 *读取/存储阅读进度条进度
 */
export function getProgress(fileName) {
  return getBookObject(fileName, 'progress');
}

export function saveProgress(fileName, progress) {
  setBookObject(fileName, 'progress', progress);
}

export function getNavigation(fileName) {
  return getBookObject(fileName, 'navigation');
}

export function saveNavigation(fileName, navigation) {
  setBookObject(fileName, 'navigation', navigation);
}

export function getMetadata(fileName) {
  return getBookObject(fileName, 'metadata');
}

export function saveMetadata(fileName, metadata) {
  setBookObject(fileName, 'metadata', metadata);
}

export function getCover(fileName) {
  return getBookObject(fileName, 'cover');
}

export function saveCover(fileName, cover) {
  setBookObject(fileName, 'cover', cover);
}

export function getTheme(fileName) {
  return getBookObject(fileName, 'theme');
}

export function saveTheme(fileName, theme) {
  setBookObject(fileName, 'theme', theme);
}

export function getFontSize(fileName) {
  return getBookObject(fileName, 'fontSize');
}

export function saveFontSize(fileName, fontSize) {
  setBookObject(fileName, 'fontSize', fontSize);
}
